﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler
{
    public class GetDepCommandHandler : IRequestHandler<GetDepCommand, APIResult<List<User>>>
    {

        private readonly IBaseRepository<User> userRep;

        public GetDepCommandHandler(IBaseRepository<User> userRep)
        {
            this.userRep = userRep;
        }

        /// <summary>
        /// 根据部门查询用户名称
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<User>>> Handle(GetDepCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<User>> result = new APIResult<List<User>>();
            var list = userRep.GetAll().Where(p=>request.DepId.Contains(p.DepId.ToString()));
            if (list != null)
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = list.ToList();
            }
            else
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
